package
{
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;

	/**
	 * 이퓨전아이 디버깅 
	 * 
	 * 문자열 데이터를 보내주는 LocalConnection 코어 
	 * @author HyungChul
	 * 
	 */
	
	public class DebugerCore
	{
		private var NAME: String = "_efustioni_debuger"; 
		
		private var localConnection: LocalConnection;
		private var localState: Boolean;
		
		static public const INIT: String = "init";
		static public const LOG: String = "log";
		static public const ERROR: String = "error";
		
		static public const COLOR: String = "color";
		
		public function DebugerCore()
		{			
			this.localConnection = new LocalConnection();
			this.localConnection.allowDomain("*");
			this.localConnection.addEventListener(StatusEvent.STATUS, statusEH );
			
			try
			{
				this.localConnection.connect( NAME );
				this.localConnection.close();
			}
			catch( e: Error )
			{
				localState = true;
				this.localConnection.send( NAME, "startHandler" , "start" );
			}
		}
		
		public function log ( $msg: String, $color: uint = 0 ): void
		{
			if ( $color > 0 )
			{
				call ( $msg, LOG );
			}else
			{
				call ( $msg, COLOR, $color );
			}
			
		}
		
		public function error ( $msg: String ): void
		{
			call ( $msg, LOG );
		}
		
		public function init ( $msg: String ): void
		{
			call ( $msg, INIT );
		}
		
		
		private function statusEH ( e: StatusEvent ) : void
		{
			trace ( " [ # status Code # ] " + e.code );
			trace ( " [ # status level # ] " + e.level );
		}
		
		public function call( $msg: Object, $type: String, $color: Number = 0  ): void
		{
			if( this.localState )
			{			
				this.localConnection.send( NAME, "message", $msg, $type, $color );
			}else
			{
				trace( $msg );
			}
		}			
	}
}